package org.dandelero.properties.framework;

import org.apache.commons.lang.StringUtils;

/**
 * A predicate that filters property keys (strings) with a specific suffix.
 * 
 * @author dandelero
 */
public class KeyEndsWithPredicate extends AbstractStringPredicate {

	// ==================================================
	// Fields.
	// ==================================================

	/**
	 * The acceptable suffix.
	 */
	private final String desiredSuffix;

	// ==================================================
	// Initialisation.
	// ==================================================

	/**
	 * Constructs a predicate that checks whether a string ends with a desired suffix.
	 * 
	 * @param desiredSuffix
	 *            the acceptable suffix.
	 */
	public KeyEndsWithPredicate(String desiredSuffix) {
		this.desiredSuffix = desiredSuffix;
	}

	// ==================================================
	// AbstractStringPredicate implementation.
	// ==================================================

	/**
	 * The check as to whether a property should be accepted or rejected.
	 * 
	 * @param s
	 *            the key.
	 * @return true or false.
	 */
	public boolean evaluate(String s) {
		if (StringUtils.isEmpty(s)) {
			return false;
		}

		return s.endsWith(desiredSuffix);
	}

}
